package com.ylz.sjzx.sio.room.repository;

import com.ylz.sjzx.sio.common.mid.cache.redis.JedisTemplate;
import com.ylz.sjzx.sio.common.core.constant.Const;
import com.ylz.sjzx.sio.common.core.utils.GsonUtil;
import com.ylz.sjzx.sio.room.entity.HistoryMessage;
import com.ylz.sjzx.sio.sdk.client.session.IMSession;
import lombok.SneakyThrows;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/*
 * 正式场景下，使用redis或者数据库来存储session信息
 */
public final class Room2Repository {

    /**
     * 历史问题
     *
     * @param serssion
     */
    @SneakyThrows
    public static Map historyQuestion(IMSession serssion) {
        return JedisTemplate.me().hgetAll(Const.PREFIX_YKT + serssion.getRoomName() + ":question");
    }

    /**
     * 保存聊天消息
     *
     * @return
     */
    @SneakyThrows
    public static void saveMessage(IMSession serssion, HistoryMessage message) {
        StringBuilder key = new StringBuilder();
        key.append("ykt:")
                .append(message.getRoomName())
                .append(":messageByTime");
        JedisTemplate.me().zSet(key.toString(), GsonUtil.bean2Json(message), System.currentTimeMillis());
    }

    /**
     * 保存聊天消息
     *
     * @return
     */
    @SneakyThrows
    public static void saveMessageList(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
        JedisTemplate.me().zSetList(key, tuples);
    }

    /**
     * 保存聊天消息
     *
     * @return
     */
    @SneakyThrows
    public static Set<String> zrangeByScore(IMSession serssion, Double score) {
        String key = "ykt:${roomName}:messageByTime".replace("${roomName}", serssion.getRoomName());
        return JedisTemplate.me()
                .zrangeByScore(key, score.longValue(), score.longValue());
    }

    /**
     * 获取聊天消息
     *
     * @param serssion
     * @param start
     * @param end
     * @return
     */
    @SneakyThrows
    public static Set<String> getMessage(IMSession serssion, long start, long end) {
        return JedisTemplate.me()
                .zrange("ykt:${roomName}:messageByTime".replace("${roomName}", serssion.getRoomName()), start, end);
    }

    @SneakyThrows
    public static Set<String> getMessageByScore(IMSession serssion, long start, long end) {
        return JedisTemplate.me()
                .rangeByScore("ykt:${roomName}:messageByTime".replace("${roomName}",
                        serssion.getRoomName()), start, end);
    }

    @SneakyThrows
    public static void removeMessage(IMSession serssion, String member) {
        JedisTemplate.me()
                .zrem("ykt:${roomName}:messageByTime".replace("${roomName}", serssion.getRoomName()), member);
    }

    /**
     * 保存白板
     *
     * @return
     */
    @SneakyThrows
    public static void saveWb(String key, Object message) {
        JedisTemplate.me()
                .listPushTail(key, 0, GsonUtil.bean2Json(message));
    }

    /**
     * 获取
     *
     * @return
     */
    @SneakyThrows
    public static List<String> getPpt(IMSession serssion, long start, long end) {
        return JedisTemplate.me()
                .listRange("wb:${roomName}:ppt".replace("${roomName}", serssion.getRoomName()), start, end);
    }

    /**
     * 获取 pptIndedx
     *
     * @param serssion
     * @return
     */
    @SneakyThrows
    public static List pptIndex(IMSession serssion) {
        return JedisTemplate.me()
                .get("wb:${room}:ppt_index".replace("${room}", serssion.getRoomName()), List.class);
    }

    @SneakyThrows
    public static List pptIndex(IMSession serssion, String pptId, String pageIndex) {
        String key = "wb:" + serssion.getRoomName() + ":" + pptId + "_" + pageIndex;
        List list = JedisTemplate.me()
                .listRange(key, 0, 0);
        return list;
    }

    /**
     * 保存 pptIndedx
     *
     * @param session
     * @return
     */
    public static void savePptIndex(IMSession session, Map<String, Object> param) {
        List list = Optional.ofNullable(pptIndex(session)).orElseGet(ArrayList::new);
        list.add("wb:${room}:ppt".replace("${room}", session.getRoomName()));
        StringBuilder sb = new StringBuilder();
        sb.append("wb:" + session.getRoomName());
        sb.append(":" + param.get("pptId"));
        sb.append("_" + param.get("slide"));
        list.add(sb.toString());
        Set<String> set = new HashSet<>(list);
        JedisTemplate.me()
                .set("wb:${room}:ppt_index".replace("${room}", session.getRoomName()), set);
    }
}
